package com.fick.struts.action;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.fick.hibernate.dao.ChannelDAO;
import com.fick.hibernate.dao.VodDAO;
import com.fick.hibernate.dao.VodServerDAO;
import com.fick.hibernate.dao.VodsDAO;
import com.fick.hibernate.model.Admin;
import com.fick.hibernate.model.Channel;
import com.fick.hibernate.model.Vod;
import com.fick.hibernate.model.VodServer;
import com.fick.hibernate.model.Vods;

/**
 * 视频管理 add by 王浩强 2009-08-18
 * */
public class VodAction extends DispatchAction {

	private VodDAO dao = new VodDAO();

	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String id = request.getParameter("id");
		String channelid = request.getParameter("channelid");
		Channel channel = null;
		try {
			channel = new ChannelDAO().findById(Integer.parseInt(channelid));
		} catch (Exception e) {
		}
		String serverId = request.getParameter("vodServer");
		VodServer server = null;
		try {
			server = new VodServerDAO().findById(Integer.parseInt(serverId));
		} catch (Exception e) {
		}
		String name = request.getParameter("name");
		String img = request.getParameter("img");
		String format = request.getParameter("format");

		Admin admin = (Admin) request.getSession().getAttribute("admin");
		if (id != null && !id.equals("0") && id.trim().length() > 0) {
			// 修改操作
			Vod vod = dao.findById(Integer.parseInt(id));
			if (vod != null) {
				try {
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
					Date date = new Date();
					if (request.getParameter("date") != null && request.getParameter("date").trim().length() > 0) {
						date = sdf.parse(request.getParameter("date"));
					}
					vod.setAddTime(date);
				} catch (Exception e) {
					vod.setAddTime(new Date());
				}
				vod.setAdmin(admin);
				if (channel != null) {
					vod.setChannel(channel);
				}
				if (server != null) {
					vod.setServer(server);
				}
				vod.setFormat(format);
				vod.setName(name);
				if (vod.getImg() != null && vod.getImg().trim().length() > 0 && !vod.getImg().equals(img)) {
					File file = new File(request.getSession().getServletContext().getRealPath("/" + vod.getImg()));
					if (file.exists()) {
						file.delete();
					}
				}
				vod.setImg(img);
				dao.update(vod);
				response.sendRedirect("/admin/treeVod.jsp");
			}
		} else {
			// 添加操作
			Vod vod = new Vod();
			try {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				Date date = new Date();
				if (request.getParameter("date") != null && request.getParameter("date").trim().length() > 0) {
					date = sdf.parse(request.getParameter("date"));
				}
				vod.setAddTime(date);
			} catch (Exception e) {
				vod.setAddTime(new Date());
			}
			vod.setAdmin(admin);
			if (channel != null) {
				vod.setChannel(channel);
			}
			if (server != null) {
				vod.setServer(server);
			}
			vod.setFormat(format);
			vod.setName(name);
			vod.setImg(img);
			dao.save(vod);
			// 把vods里vod为null的设置为此vod
			VodsDAO vodsDAO = new VodsDAO();
			List<Vods> vodsList = vodsDAO.findBySql(Vods.class, "where vod=null");
			if (vodsList != null && vodsList.size() > 0) {
				for (int i = 0; i < vodsList.size(); i++) {
					Vods vods = vodsList.get(i);
					vods.setVod(vod);
					vodsDAO.update(vods);
				}
			}
			response.sendRedirect("/admin/vod.jsp");
		}
		return null;
	}

	public ActionForward delAll(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String str = request.getParameter("str");
		Vod vod;
		String[] ids = str.split(",");
		int id = 0;
		try {
			for (int i = 0; i < ids.length; i++) {
				id = Integer.parseInt(ids[i]);
				vod = dao.findById(id);
				if (vod != null) {
					if (vod.getImg() != null && vod.getImg().trim().length() > 0) {
						File file = new File(request.getSession().getServletContext().getRealPath("/" + vod.getImg()));
						if (file.exists()) {
							file.delete();
						}
					}
					dao.delete(vod);
				}
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
			log.error(e);
		}
		response.sendRedirect("/admin/treeVod.jsp");
		return null;
	}

	public ActionForward delById(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String id = request.getParameter("id");
		if (id != null && id.trim().length() > 0) {
			Vod vod = dao.findById(Integer.parseInt(id));
			if (vod != null) {
				if (vod.getImg() != null && vod.getImg().trim().length() > 0) {
					File file = new File(request.getSession().getServletContext().getRealPath("/" + vod.getImg()));
					if (file.exists()) {
						file.delete();
					}
				}
				dao.delete(vod);
			}
		}
		response.sendRedirect("/admin/treeVod.jsp");
		return null;
	}
}
